var backgroundAudioManager
var index
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainView: 1,
    date: "",
    url: getApp().globalData.source + "/day_news_jianbao/zhjb.png",
    data: [],
    currentPlayIndex: -1
  },
  tabItemTap: function(e) {
    var that = this;
    var _dataSet = e.currentTarget.dataset;
    this.setData({
      mainView: _dataSet.view
    })
    if (_dataSet.view == 1) {
      this.getAPINewsData("male")
    } else {
      this.getAPINewsData("female")
    }
  },

  itemclick: function(e) {
    index = e.currentTarget.dataset.index;
    this.setData({
      currentPlayIndex: index
    })
    this.play(index)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    this.getAPINewsData("male");
    this.getNowFormatDate()

  },
  stop: function(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      currentPlayIndex: -1
    })
    backgroundAudioManager.stop()
  },
  getNowFormatDate: function() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + "年" + month + "月" + strDate + "日";
    this.setData({
      date: currentdate
    })
    // return currentdate;
  },
  getAPINewsData: function(sex) {
    var vm = this
    var data = {
      "gender": sex,
      "keyword": ""
    }

    getApp().requestLoading("https://skill-multiple.superbriefing.heard-gl.com/api/v1/tiantian/superbriefing",
      "POST", data, "加载中..",
      function(res) {
        console.log(res)
        if (res.code == 200) {
          console.log(res.directives)
          vm.setData({
            info: res.directives
          })

          for (var i = 0; i < res.directives.length; i++) {
            if (wx.getStorageSync("jianbao") != undefined) {
              if (res.directives[i].categoryId == wx.getStorageSync("jianbao")) {
                if (wx.getStorageSync("sex") != undefined) {
                  console.log(wx.getStorageSync("sex"));
                  console.log(vm.data.mainView);
                  if (vm.data.mainView == wx.getStorageSync("sex")) {
                    res.directives[i].checked = true
                    vm.setData({
                      currentPlayIndex: i
                    })
                  } else {
                    vm.setData({
                      currentPlayIndex: -1
                    })
                  }
                }

              }
            }
            switch (res.directives[i].categoryId) {
              // (综合 - 43 全网热议 - 45 科技 - 46 体育 - 47 商业 - 44 明星 - 50 房产 - 55 汽车 - 49 时尚 - 53 历史上的今天 - 62 电影 - 51 NBA - 57 数码 - 52 教育 - 54 电竞 - 60 二次元 - 56 大公司不开心 - 59 企鹅动态 61)
              case 49:
                res.directives[i].image = getApp().globalData.source + "day_news_jianbao/car.jpg"
                break
              case 43:
                res.directives[i].image = getApp().globalData.source + "day_news_jianbao/synthetical.jpg"
                break
              case 45:
                res.directives[i].image = getApp().globalData.source + "day_news_jianbao/quan_wang_hot.jpg"
                break
              case 46:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/ke_ji.jpg"
                break
              case 47:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/sports.jpg"
                break
              case 44:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/business.jpg"
                break
              case 55:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/house.jpg"
                break
              case 51:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/movie.jpg"
                break
              case 57:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/NBA.jpg"
                break
              case 52:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/shu_ma.jpg"
                break
              case 54:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/education.jpg"
                break
              case 60:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/dian_jing.jpg"
                break
              case 53:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/shi_shang.jpg"
                break
              case 62:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/history_day.jpg"
                break
              case 56:
                res.directives[i].image = getApp().globalData.source + "/day_news_jianbao/er_ci_yuan.jpg"
                break
            }
          }
          vm.setData({
            info: res.directives
          })
        }
      },
      function(res) {}
    )
  },

  play: function(index) {
    console.log(index)
    var vm = this
    index=index;
    wx.getBackgroundAudioManager().pause();
    if(backgroundAudioManager==undefined){
      backgroundAudioManager = wx.createInnerAudioContext();
    }
    var voicePath = this.data.info[index].audioResource[0].url;
    backgroundAudioManager.src = voicePath;
    backgroundAudioManager.play();
    wx.setStorageSync("jianbao", this.data.info[index].categoryId)
    wx.setStorageSync("sex", this.data.mainView)
    vm.setData({
      currentPlayIndex: index
    })
    backgroundAudioManager.onEnded(function() {
      // vm.setData({
      //   currentPlayIndex: index
      // })
      vm.play(index)
    })

    backgroundAudioManager.onStop(function() {
      // vm.setData({
      //   currentPlayIndex: -1
      // })
      wx.setStorageSync("jianbao", undefined)
      wx.setStorageSync("sex", undefined)
    })

    backgroundAudioManager.onPause(function() {
      // vm.setData({
      //   currentPlayIndex: -1
      // })
      wx.setStorageSync("jianbao", undefined)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    backgroundAudioManager.stop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})